<template>
  <div class="login-container">
    <div class="left">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >

        <div class="title-container">
          <div class="title">
            <img :src="require('@/assets/images/downlod.png')" alt="">
            <span class="titleLeft">黑马面面</span>
            <span class="titleRight">用户登陆</span>
          </div>
        </div>

        <el-form-item prop="phone">
          <span class="svg-container">
            <i class="el-icon-user" />
          </span>
          <el-input
            ref="phone"
            v-model="loginForm.phone"
            placeholder="请输入手机号"
            name="phone"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <i class="el-icon-lock" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>

        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <span class="svg-container">
                <i class="el-icon-key" />
              </span>
              <el-input
                ref="code"
                v-model="loginForm.code"
                placeholder="请输入验证码"
                name="code"
                type="text"
                tabindex="1"
                auto-complete="on"
              />
            </el-col>
            <el-col :span="8">
              <img :src="src" alt="" style="height: 40px; width: 100%;" @click="getCode">
            </el-col>
          </el-row>
        </el-form-item>

        <el-button
          :loading="loading"
          type="primary"
          style="width:100%;margin-bottom:30px;"
          @click.native.prevent="handleLogin"
        >登陆</el-button>

      </el-form>
    </div>
    <img :src="require('@/assets/images/login_banner_ele.png')" alt="">
  </div>
</template>

<script>
import { CodeAPI } from '@/api/login'
// import { login } from '@/api/user'
// import Axios from 'axios'
export default {
  name: 'Login',
  data() {
    return {
      src: '',
      loginForm: {
        phone: '18511111111',
        password: '12345678',
        code: ''
      },
      loginRules: {
        phone: [
          { required: true, trigger: 'blur', message: '请输入手机号' },
          { pattern: /^(?:(?:\+|00)86)?1\d{10}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入密码' },
          { min: 6, max: 8, massage: '密码需6-8位', trigger: 'blur' }
        ],
        code: [
          { required: true, trigger: 'blur', message: '请输入验证码' }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  created() {
    this.getCode()
  },
  methods: {
    async getCode() {
      const res = await CodeAPI()
      // console.log(res)
      this.src = `data:image/png;base64,${btoa(new Uint8Array(res.data).reduce((data, byte) => data + String.fromCharCode(byte), ''))}`
      // console.log(this.src)
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 表单验证
    handleLogin() {
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/Login', this.loginForm)
          this.loading = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      // padding: 12px 5px 12px 15px;
      color: #ccc;
      caret-color: #666;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #ddd;
    background: #fff;
    border-radius: 5px;
    color: #ddd;
    height: 40px;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background: linear-gradient(to bottom, #1493fa, #01c6fa);
  overflow: hidden;
  display: flex;
  justify-content: space-evenly;
  align-items: center;

  .left {
    width: 480px !important;
  }

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 42px;
    margin: 0 auto;
    overflow: hidden;
    background-color: #f5f5f5;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    // padding: 6px 5px 6px 15px;
    padding: 0 0 5px 10px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: #0c0c0c;
      margin: 0px auto 40px auto;

      span {
        padding: 0 10px;
      }

      .titleLeft {
        border-right: 1px solid #d4d4d4;
      }

      .titleRight {
        font-size: 24px;
      }
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  ::v-deep{
    .el-form-item__content{
    line-height: normal;
  }
  }
}
</style>
